<?php

namespace app\admin\controller;

use app\BaseController;

use think\facade\View;

use think\facade\Db;

use think\facade\Session;
use app\admin\facade\ThinkAddons;
use think\facade\Event;
use think\facade\Config as ConfigFile;



class Login extends BaseController

{

    public function index()

    {


        //var_dump($this->request->isPost());exit();

        /* if($this->request->isPost()){

    		$this->error('111');

    	} */

        // if (Session::has('admin')) {

        //     $this->error(__('You are logged'),  url('index/index'));

        // }
        // dd(ThinkAddons::localAddons('theme'));


        //判断是否启用社会化登入

        $social_login = whetherToUsePlugin('social_login') ? true : false;
        $param = ['socail_login' => $social_login];
        $login_theme = ConfigFile::get('site.login_theme');
        $theme = $login_theme ? ThinkAddons::theme($login_theme) : false;
        if ($theme) {
            return $theme->theme($param);
        } else {
            return View::fetch('', $param);
        }
    }



    public function signin()

    {

        // if (Session::has('admin')) {

        //     $this->error(__('You are logged'), 'index/index');

        // }

        $username = $this->request->param('username');

        $password = $this->request->param('password');

        $keep_login = $this->request->param('keep_login');

        //拖动验证

        //      $captcha = $this->request->param('captcha');

        //    	if(!captcha_check($captcha)){

        //    		// 验证失败

        //    		$this->error(__('Verification code error'));

        //    	};



        $admininfo = Db::name('admin')->where('username', $username)->find();

        if (empty($admininfo)) {

            $this->error(__('Incorrect username or password'));
        }

        if ($admininfo['status'] != 'normal') {

            $this->error(__('Account is disabled'));
        }

        if ($admininfo['password'] != md5(md5($password) . $admininfo['salt'])) {

            $this->error(__('Incorrect username or password'));
        }



        // 查找规则

        $rules = Db::name('auth_group_access')

            ->alias('a')

            ->leftJoin('auth_group ag', 'a.group_id = ag.id')

            ->field('a.group_id,ag.rules,ag.title')

            ->where('uid', $admininfo['id'])

            ->find();

        $admininfo['expire_time'] = $keep_login == 1 ? true : time() + 7200;

        Session::set('admin', $admininfo);



        Session::set('admin.group_id', $rules['group_id']);


        Session::set('admin.rules', explode(',', $rules['rules']));

        Session::set('admin.title', $rules['title']);





        $this->success(__('Login successful'));
    }

    public function logout()

    {

        Session::delete('admin');

        return redirect('index');
    }



    public function test()

    {

        if (!$this->request->isAjax()) {

            $list = [

                ['user_id' => 1, 'name' => '武则天'],

                ['user_id' => 2, 'name' => '小乔'],

                ['user_id' => 3, 'name' => '司马懿'],

                ['user_id' => 4, 'name' => '妲己'],

                ['user_id' => 5, 'name' => '张良'],

            ];

            $data = [

                'code'  =>  1,

                'msg'   =>  null,

                'data'  =>  $list,

            ];

            return json($data);
        }

        View::assign('role_id', 3);

        return View::fetch();
    }



    /**

     * 调用方法demo 默认微信登入

     * @param string $type 登入类型 'config/social.php 配置文件'  //'Qq', 'Weixin', 'Sina', 'Baidu', 'Gitee', 'Github', 'Google', 'Facebook', 'Taobao', 'Oschina', 'Douyin', 'Xiaomi', 'Dingtalk'

     */

    public function social_login($type = 'Weixin')

    {

        $type = input()['type'];

        //钩子事件 短信插件

        $plugin_name = 'Aaliyun';

        Event::listen($plugin_name, 'addons\social_login\event\SocialLogin');

        $hoddok_res = event($plugin_name, $type);

        //        var_dump($hoddok_res);



        //登入成功 登入注册等用户信息业务逻辑

    }
}
